<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>对称二叉树算法解析 | 算法可视化</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .code-block {
            background-color: #1e293b;
            border-left: 4px solid #7c3aed;
        }
        .concept-card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .concept-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .first-letter {
            font-size: 3.5em;
            line-height: 0.8em;
            float: left;
            margin-right: 8px;
            color: #4f46e5;
            font-weight: 700;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-4">对称二叉树</h1>
                    <p class="text-xl md:text-2xl font-light mb-6 opacity-90">优雅解决二叉树镜像对称问题</p>
                    <div class="flex items-center space-x-4">
                        <span class="px-3 py-1 bg-white bg-opacity-20 rounded-full text-sm font-medium">递归</span>
                        <span class="px-3 py-1 bg-white bg-opacity-20 rounded-full text-sm font-medium">迭代</span>
                        <span class="px-3 py-1 bg-white bg-opacity-20 rounded-full text-sm font-medium">深度优先</span>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <div class="bg-white bg-opacity-10 p-6 rounded-xl backdrop-blur-sm border border-white border-opacity-20">
                        <div class="flex justify-center">
                            <div class="flex items-center justify-center">
                                <div class="relative">
                                    <div class="w-16 h-16 rounded-full bg-white bg-opacity-20 flex items-center justify-center">
                                        <i class="fas fa-code text-2xl"></i>
                                    </div>
                                    <div class="absolute -right-2 -bottom-2 w-8 h-8 rounded-full bg-indigo-300 flex items-center justify-center">
                                        <i class="fas fa-project-diagram text-xs"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <p class="text-center mt-4 text-sm opacity-90">通过递归和迭代两种方式，探索二叉树对称性的奥秘</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-6 py-12">
        <!-- Problem Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-indigo-100 rounded-full flex items-center justify-center mr-3">
                    <i class="fas fa-question text-indigo-600"></i>
                </span>
                题目描述
            </h2>
            <div class="bg-white rounded-xl shadow-sm p-6">
                <p class="text-gray-700 leading-relaxed">
                    <span class="first-letter">给</span>定一个二叉树，检查它是否是镜像对称的。例如，二叉树 [1,2,2,3,4,4,3] 是对称的，而 [1,2,2,null,3,null,3] 不是。一棵对称二叉树满足：它的左右两个子树呈镜像关系。
                </p>
            </div>
        </section>

        <!-- Algorithm Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-indigo-100 rounded-full flex items-center justify-center mr-3">
                    <i class="fas fa-cogs text-indigo-600"></i>
                </span>
                算法解析
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <!-- Recursive Method -->
                <div class="concept-card bg-white rounded-xl shadow-sm p-6">
                    <h3 class="text-xl font-bold mb-4 text-indigo-600 flex items-center">
                        <i class="fas fa-undo-alt mr-2"></i>
                        递归法
                    </h3>
                    <p class="text-gray-700 mb-4">检查左右子树是否镜像对称，比较左子树的左孩子与右子树的右孩子，以及左子树的右孩子与右子树的左孩子。</p>
                    <div class="mb-4">
                        <span class="text-sm font-medium text-gray-500">时间复杂度: <span class="text-indigo-600">O(n)</span></span>
                        <span class="mx-2 text-gray-300">|</span>
                        <span class="text-sm font-medium text-gray-500">空间复杂度: <span class="text-indigo-600">O(h)</span></span>
                    </div>
                    <div class="code-block rounded-lg p-4 text-gray-100 font-mono text-sm overflow-x-auto">
                        <pre><code>def isSymmetric(root):
    def isMirror(left, right):
        if not left and not right:
            return True
        if not left or not right:
            return False
        return (left.val == right.val and 
                isMirror(left.left, right.right) and 
                isMirror(left.right, right.left))
    
    return isMirror(root, root) if root else True</code></pre>
                    </div>
                </div>
                
                <!-- Iterative Method -->
                <div class="concept-card bg-white rounded-xl shadow-sm p-6">
                    <h3 class="text-xl font-bold mb-4 text-indigo-600 flex items-center">
                        <i class="fas fa-sync-alt mr-2"></i>
                        迭代法
                    </h3>
                    <p class="text-gray-700 mb-4">使用队列，成对比较左右子树的对应节点。</p>
                    <div class="mb-4">
                        <span class="text-sm font-medium text-gray-500">时间复杂度: <span class="text-indigo-600">O(n)</span></span>
                        <span class="mx-2 text-gray-300">|</span>
                        <span class="text-sm font-medium text-gray-500">空间复杂度: <span class="text-indigo-600">O(w)</span></span>
                    </div>
                    <div class="bg-indigo-50 rounded-lg p-4 text-gray-800">
                        <p class="text-sm mb-2 font-medium"><i class="fas fa-lightbulb text-indigo-500 mr-2"></i>实现思路</p>
                        <ul class="list-disc pl-5 text-sm space-y-1">
                            <li>初始化队列，将根节点的左右子节点加入队列</li>
                            <li>每次取出两个节点进行比较</li>
                            <li>将左节点的左子节点与右节点的右子节点入队</li>
                            <li>将左节点的右子节点与右节点的左子节点入队</li>
                            <li>重复直到队列为空</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-indigo-100 rounded-full flex items-center justify-center mr-3">
                    <i class="fas fa-project-diagram text-indigo-600"></i>
                </span>
                算法可视化
            </h2>
            <div class="bg-white rounded-xl shadow-sm p-6">
                <div class="mermaid">
                    graph TD
                        A[根节点] --> B[左子树]
                        A --> C[右子树]
                        B --> D[左子树左节点]
                        B --> E[左子树右节点]
                        C --> F[右子树左节点]
                        C --> G[右子树右节点]
                        D -- 比较 --> G
                        E -- 比较 --> F
                </div>
                <p class="mt-4 text-gray-600 text-sm">图示展示了对称二叉树的比较过程：左子树的左节点与右子树的右节点比较，左子树的右节点与右子树的左节点比较。</p>
            </div>
        </section>

        <!-- Key Points Section -->
        <section>
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-indigo-100 rounded-full flex items-center justify-center mr-3">
                    <i class="fas fa-key text-indigo-600"></i>
                </span>
                核心要点
            </h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white p-6 rounded-xl shadow-sm">
                    <div class="w-12 h-12 bg-indigo-100 rounded-lg flex items-center justify-center mb-4">
                        <i class="fas fa-sitemap text-indigo-600 text-xl"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">镜像对称定义</h3>
                    <p class="text-gray-600 text-sm">左右子树结构对称，对应节点值相同。</p>
                </div>
                <div class="bg-white p-6 rounded-xl shadow-sm">
                    <div class="w-12 h-12 bg-indigo-100 rounded-lg flex items-center justify-center mb-4">
                        <i class="fas fa-layer-group text-indigo-600 text-xl"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">递归终止条件</h3>
                    <p class="text-gray-600 text-sm">两节点都为空或其中一节点为空。</p>
                </div>
                <div class="bg-white p-6 rounded-xl shadow-sm">
                    <div class="w-12 h-12 bg-indigo-100 rounded-lg flex items-center justify-center mb-4">
                        <i class="fas fa-running text-indigo-600 text-xl"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">迭代实现优势</h3>
                    <p class="text-gray-600 text-sm">避免递归堆栈溢出，适合深度大的树。</p>
                </div>
            </div>
        </section>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            mermaid.initialize({ startOnLoad: true, theme: 'neutral' });
            
            // 微交互效果
            const codeBlocks = document.querySelectorAll('.code-block pre');
            codeBlocks.forEach(block => {
                block.addEventListener('mouseenter', () => {
                    block.parentElement.style.borderLeftColor = '#7c3aed';
                });
                block.addEventListener('mouseleave', () => {
                    setTimeout(() => {
                        block.parentElement.style.borderLeftColor = '#7c3aed';
                    }, 300);
                });
            });
        });
    </script>
</body>
</html>